<?php
namespace app\api\controller;
use app\common\controller\BaseApi;

/*勤鸟软件  对接*/
class QNApi extends  BaseApi
{
	//进出门禁
	const GetContractInfo="http://zwzlcloud.ocmcom.com/Api/QnSync/GetContractInfo";// 获取会员信息
	const CheckIn="http://zwzlcloud.ocmcom.com/Api/QnSync/CheckIn";//进场
	const ConfirmCheckOut="http://zwzlcloud.ocmcom.com/Api/QnSync/ConfirmCheckOut"; //出场
	
	protected $shop_id;//中未智联店铺id
	
	/**
	 * 测试地址
	 * http://SmartGYM.com/api/DXApi/GetPTEmployeeList/v/1/t/1/key/zwzl_admin/sign/5de093255ad3e78341dbe00309c9436c/shop_id/68
	 * */
	 public function _initialize()
    {
//  	parent::_initialize();
    }
	/////////////////////////////////////////获取安科数据////////////////////////////////////////////////////////////////
	/**
	 * 方法前操作获取店铺信息
	 */
	function before_method($shop_id){
		if(empty($shop_id)){
			$this->ajax_error('','',"店铺id 为空");
		}else{
			$this->shop_id=$shop_id;
		}
	}
	/**
	 * http://SmartGYM.com/api/QNApi/GetContractInfo/card_no/123456/shop_id/20
	 *
	 * 获取合同号
	 * @param $card_no
	 * @return $data
	 * [
	 *	    {
	 *		        "ContractID": "06162ef0-7022-433e-80bd-7d9d44dc113c",
	 *		        "MemberID": "5c5313c8-aa5b-493a-8664-88e89b19a6ca",
	 *		        "MemberName": "ERCC",
	 *		        "Sex": "鐢�",
	 *		        "Mobile": "18523020102",
	 *		        "CardNo": "2238082314",
	 *		        "StartDate": "2017-10-19",
	 *		        "EndDate": "2018-10-28",
	 *		        "ContractStatus": 1,
	 *		        "MembershipType": 2,
	 *		        "MembershipTypeName": "骞存湀鍗�",
	 *		        "SurplusNum": "0",
	 *		        "StoredSumValue": "0.00"
	 *		    }
	 *		]
	 */
	function GetContractInfo($card_no,$shop_id){
		$this->before_method($shop_id);
		$result=http_get(self::GetContractInfo,['card_no'=>$card_no,'shop_id'=>$this->shop_id]);
		
		if($result['flag']){
			$json=json_decode($result['data'],TRUE);
			if($json['flag']){
				$json=$json['data'];
				return template_success("成功(勤鸟)",array(
														  "ContractID"=>$json['ContractID'],
														  "MemberID"=>$json['MemberID'],
														  "StartDate"=>$json['StartDate'],
														   "MemberName"=>$json['MemberName'],
														  "EndDate"=>$json['EndDate'],
														  "ContractStatus"=>$json['ContractStatus'],
														  "Sex"=>$json['Sex'],
														  "Mobile"=>$json["Mobile"],
														  "CardNo"=>$json['CardNo'],
														  "MembershipType"=>$json['MembershipType'],
														  "MembershipTypeName"=>$json['MembershipTypeName'],
														  "SurplusNum"=>$json['SurplusNum'],
														  "StoredSumValue"=>$json['StoredSumValue'],
														  )
														  );
			
				
			}else{
				return template_error($json['msg']);
			}			
		}else{
			return template_error($result['msg']);
		}
	}
	/**
	 * 入场操作
	 * http://SmartGYM.com/api/QNApi/CheckIn/card_no/123456/shop_id/20
	 * @param $card_no
	 */
	function CheckIn($card_no,$shop_id){
		$this->before_method($shop_id);
		$result=http_get(self::CheckIn,['card_no'=>$card_no,'shop_id'=>$this->shop_id]);
		if($result['flag']){
			$json= json_decode($result['data'],TRUE);
			if($json['flag']){
				return template_success("成功(勤鸟)");
			}else{
				return template_error($json['msg']);
			}			
		}else{
			return template_error($result['msg']);
		}
	}
   	private	function aysncronous_out_local($url,$params=array()){
	  $host =$url;
	  if(!empty($params)&&is_array($params)){
			$host.="?t=1";
			foreach ($params as $key => $value) {
				$host.="&".$key."=".$value;
			}
		}
		  $ch = curl_init();
		  curl_setopt($ch,CURLOPT_URL,$host);
		  curl_setopt($ch,CURLOPT_TIMEOUT,1);
		  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
		  $result = curl_exec($ch);
		  curl_close($ch);
		  return $result;
	}
	/**
	 * 出场操作
	 * @param $card_no
	 * http://smartgymdemo.ocmcom.com/api/QNApi/ConfirmCheckOut/card_no/0722919051/shop_id/20
	 */
	function ConfirmCheckOut($card_no,$shop_id){
		$this->before_method($shop_id);
		$this->aysncronous_out_local(self::ConfirmCheckOut,['card_no'=>$card_no,'shop_id'=>$shop_id]);
		return template_success("成功(勤鸟)");
//		$result=http_get(self::ConfirmCheckOut,['card_no'=>$card_no,'shop_id'=>$this->shop_id]);
//		if($result['flag']){
//			$json= json_decode($result['data'],TRUE);
//			if($json['flag']){
//				return template_success("成功(勤鸟)");
//			}else{
//				return template_error($result['msg']);
//			}			
//		}else{
//			return template_error($result['msg']);
//		}
	}
	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
}
